<script setup>
import { getArticles, delArticle } from '@/api/article'
import { Edit, Delete } from '@element-plus/icons-vue'
import ChannelSelect from './components/ChannelSelect.vue'
import { formatTime } from '@/utils/format.js'
import { ref } from 'vue'
import ArticleEdit from './components/ArticleEdit.vue'
const articleEditRef = ref()
const onEditArticle = (row) => {
  articleEditRef.value.open(row)
}
const onAddArticle = () => {
  articleEditRef.value.open({})
}
const onDeleteArticle = (row) => {
  delArticle(row.id)
  getArticleList(params.value)
}
const articleList = ref([])
const total = ref(0)
// 默认请求参数对象
const params = ref({
  pagenum: 1,
  pagesize: 5,
  cate_id: '',
  state: ''
})
const loading = ref(false)
const getArticleList = async (obj) => {
  loading.value = true
  const res = await getArticles(obj)
  // console.log(res)
  total.value = res.data.total
  articleList.value = res.data.data
  loading.value = false
}
getArticleList(params.value)
// 处理分页逻辑
const SizeChange = (value) => {
  params.value.pagesize = value
  params.value.pagenum = 1
  getArticleList(params.value)
}
const CurrentChange = (value) => {
  params.value.pagenum = value
  getArticleList(params.value)
}
const onsearch = () => {
  params.value.pagenum = 1
  getArticleList(params.value)
}
const onreset = () => {
  params.value.pagenum = 1
  params.value.pagesize = 5
  params.value.cate_id = ''
  params.value.state = ''
  getArticleList(params.value)
}

// 添加或编辑成功
const onSuccess = (type) => {
  if (type === 'add') {
    // 如果是添加最好渲染最后一页
    params.value.pagenum = Math.ceil((total.value + 1) / params.value.pagesize)
    getArticleList(params.value)
  } else {
    getArticleList(params.value)
  }
}
</script>
<template>
  <page-container title="文件管理" v-loading="loading">
    <template #extra>
      <el-button class="extra" type="primary" @click="onAddArticle">
        添加文章
      </el-button>
    </template>
    <!-- 表单区域 -->
    <el-form inline style="width: 100%">
      <el-form-item label="文章分类">
        <!-- vue2=>:value @input的简写 -->
        <!-- vue3=> :modelValue @update:modelValue -->
        <!-- v-model:cid="params.cate_id -->
        <!-- <ChannelSelect v-model="params.cate_id"></ChannelSelect> -->
        <ChannelSelect
          v-model:cid="params.cate_id"
          width="13vw"
        ></ChannelSelect>
      </el-form-item>
      <el-form-item label="发布状态">
        <el-select style="width: 13vw" v-model="params.state">
          <!-- 后台标记状态就是用中文 已发布/草稿 -->
          <el-option label="已发布" value="已发布"> </el-option>
          <el-option label="草稿" value="草稿"> </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onsearch">搜索</el-button>
        <el-button @click="onreset">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 表格区域 -->
    <el-table :data="articleList" style="width: 100%">
      <el-table-column label="文章标题" prop="title">
        <template #default="{ row }">
          <el-link :underline="false" type="primary">{{ row.title }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="分类" prop="cate_name"> </el-table-column>
      <el-table-column label="发表时间" prop="pub_date">
        <template #default="{ row }">
          {{ formatTime(row.pub_date) }}
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="state"> </el-table-column>
      <!-- 利用作用域插槽可以获取当前行的数据=>v-for遍历item -->
      <el-table-column label="操作">
        <template #default="{ row }">
          <el-button
            type="primary"
            :icon="Edit"
            plain
            circle
            @click="onEditArticle(row)"
          ></el-button>
          <el-button
            type="danger"
            :icon="Delete"
            plain
            circle
            @click="onDeleteArticle(row)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      v-model:current-page="params.pagenum"
      v-model:page-size="params.pagesize"
      :page-sizes="[2, 3, 5, 10]"
      :background="true"
      layout="jumper,total, sizes, prev, pager, next,"
      :total="total"
      @size-change="SizeChange"
      @current-change="CurrentChange"
      style="margin-top: 2vh; justify-content: flex-end"
    />
    <!-- 编辑抽屉 -->
    <ArticleEdit ref="articleEditRef" @success="onSuccess"></ArticleEdit>
  </page-container>
</template>

<style lang="scss" scoped></style>
